

/* ================ 深拷贝 ================ */
//返回传递给他的任意对象的类
const isClass = (o) => {
  if (o === null) return "Null";
  if (o === undefined) return "Undefined";
  return Object.prototype.toString.call(o).slice(8, -1);
}

//深度克隆
const deepClone = (obj) => {
  if (!obj) { return null }
  let result, oClass = isClass(obj);
  //确定result的类型
  if (oClass === "Object") {
    result = {};
  } else if (oClass === "Array") {
    result = [];
  } else {
    return obj;
  }

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      let copy = obj[key];
      if (isClass(copy) == "Object") {
        result[key] = deepClone(copy);//递归调用
      } else if (isClass(copy) == "Array") {
        result[key] = deepClone(copy);
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
}





// 身份证验证
function ValidateIdCard(idcard) {
	const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return reg.test(idcard);
}
 //  开户行账号格式校验

function checkOpeningBankAccount(account) {
	const reg = /^([1-9]{1})(\d{11}|\d{15}|\d{16}|\d{17}|\d{18})$/;
	return reg.test(account);
}
// 邮箱正则校验
 function  email(email){
	 const reg = /^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,6})$/;
	 return reg.test(email);
 }
 // 手机正则校验
 function  phone(phone){
	 const reg = /^1[3|4|5|7|8][0-9]{9}$/;
	 return reg.test(phone);
 }
function  time_to_sec (time) {
	 var s = '';

	 var hour = time.split(':')[0];
	 var min = time.split(':')[1];
	 var sec = time.split(':')[2];

	 s = Number(hour*3600) + Number(min*60) + Number(sec);

	 return s;
 };
// 获取当天的前七天日期函数
 function  getBeforeDate(n) {
	 console.log('开始调用日期函数了',n)
	 var n = n;
	 var d = new Date();
	 var year = d.getFullYear();
	 var mon = d.getMonth() + 1;
	 var day = d.getDate();
	 if (day <= n) {
	 if (mon > 1) {
	 mon = mon - 1;
	 }
	 else {
	 year = year - 1;
	 mon = 12;
	 }
	 }
	 d.setDate(d.getDate() - n);
	 year = d.getFullYear();
	 mon = d.getMonth() + 1;
	 day = d.getDate();
	 var s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
	 return s;
}
// 时间戳转换成日期时间
function getYMDHMS(timestamp) {
	let time = new Date(timestamp)
	let year = time.getFullYear()
	let month = time.getMonth() + 1
	let date = time.getDate()
	let hours = time.getHours()
	let minute = time.getMinutes()
	let second = time.getSeconds()
	if (month < 10) {
		month = '0' + month
	}
	if (date < 10) {
		date = '0' + date
	}
	if (hours < 10) {
		hours = '0' + hours
	}
	if (minute < 10) {
		minute = '0' + minute
	}
	if (second < 10) {
		second = '0' + second
	}
	return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}

function timestampToTime(timestamp) {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y + M + D + h + m + s;
}

function year() {
	var myDate = new Date();
	var year = myDate.getYear(); //获取当前年份(2位)
	return year
}

function month() {
	var myDate = new Date();
	var month = myDate.getMonth();
	return month
}

function setData(obj,callback) {
	let that = this;
	const handleData = (tepData, tepKey, afterKey) => {
		tepKey = tepKey.split('.');
		tepKey.forEach(item => {
			if (tepData[item] === null || tepData[item] === undefined) {
				let reg = /^[0-9]+$/;
				tepData[item] = reg.test(afterKey) ? [] : {};
				tepData = tepData[item];
			} else {
				tepData = tepData[item];
			}
		});
		return tepData;
	};
	const isFn = function(value) {
		return typeof value == 'function' || false;
	};
	Object.keys(obj).forEach(function(key) {
		let val = obj[key];
		key = key.replace(/\]/g, '').replace(/\[/g, '.');
		let front, after;
		let index_after = key.lastIndexOf('.');
		if (index_after != -1) {
			after = key.slice(index_after + 1);
			front = handleData(that, key.slice(0, index_after), after);
		} else {
			after = key;
			front = that;
		}
		if (front.$data && front.$data[after] === undefined) {
			Object.defineProperty(front, after, {
				get() {
					return front.$data[after];
				},
				set(newValue) {
					front.$data[after] = newValue;
					that.$forceUpdate();
				},
				enumerable: true,
				configurable: true
			});
			front[after] = val;
		} else {
			that.$set(front, after, val);
		}
	});
	// this.$forceUpdate();
	isFn(callback) && this.$nextTick(callback);
}
module.exports = {
	timestampToTime: timestampToTime,
	ValidateIdCard: ValidateIdCard,
	checkOpeningBankAccount: checkOpeningBankAccount,
	getYMDHMS,
	setData,
	email,
	phone,
	getBeforeDate,
	deepClone,
	isClass
};
